package com.shyroke.daydayzhuanapi.mapper;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.shyroke.daydayzhuanapi.dto.*;
import com.shyroke.daydayzhuanapi.entity.AppEntity;
import com.shyroke.daydayzhuanapi.entity.LabelEntity;
import com.shyroke.daydayzhuanapi.entity.SubjectEntity;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class SubjectMapper {
    @Autowired
    private ModelMapper modelMapper;

    public SubjectAdminDto entityToAdminDto(SubjectEntity e) {
        SubjectAdminDto dto = modelMapper.map(e, SubjectAdminDto.class);
        dto.setIsCommented(BooleanUtil.toBoolean(e.getIsCommented()));
        return dto;
    }

    public SubjectEntity addRequestToEntity(SubjectAddRequest request) {
        return modelMapper.map(request,SubjectEntity.class);
    }

    public void updateDtoToEntity(SubjectAddRequest request, SubjectEntity subject) throws Exception{
        if(StrUtil.isNotBlank(request.getTitle())){
            subject.setTitle(request.getTitle());
        }
        if(StrUtil.isNotBlank(request.getSummary())){
            subject.setSummary(request.getSummary());
        }
        if(StrUtil.isNotBlank(request.getContent())){
            String content = request.getContent();
            content = content.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
            content = content.replaceAll("\\+", "%2B");
            subject.setContent(content);
            subject.setContent(URLDecoder.decode(subject.getContent(),"utf-8"));
        }
        if(StrUtil.isNotBlank(request.getKeywords())){
            subject.setKeywords(request.getKeywords());
        }
        subject.setIsCommented(request.getIsCommented());
    }

    public SubjectDto entityToDto(SubjectEntity e) {
        SubjectDto dto = modelMapper.map(e, SubjectDto.class);
        Set<AppEntity> appList = e.getAppList();
        if(CollectionUtil.isNotEmpty(appList)){
            List<AppDto> appDtoList = appList.stream().map(app -> modelMapper.map(app, AppDto.class)).collect(Collectors.toList());
            dto.setAppList(appDtoList);
        }
        return dto;
    }

    public SubjectHomeDto entityToHomeDto(SubjectEntity subject) {
        SubjectHomeDto dto = modelMapper.map(subject, SubjectHomeDto.class);
        Set<AppEntity> appList = subject.getAppList();
        if(CollectionUtil.isNotEmpty(appList)){
            List<AppDto> appDtoList = appList.stream().map(app -> {
                AppDto appDto = modelMapper.map(app, AppDto.class);
                List<LabelEntity> labelList = app.getLabelEntityList();
                if(CollectionUtil.isNotEmpty(labelList)){
                    appDto.setFirstLableName(labelList.get(0).getLabelName());
                }
                appDto.setCreateTime2(DateUtil.format(appDto.getCreateTime(),"yyyy-MM-dd"));
                Integer device = app.getDevice();
                if(device != null){
                    switch (device){
                        case 0:
                            appDto.setShowDevice("安卓/苹果");
                            break;
                        case 1:
                            appDto.setShowDevice("安卓");
                            break;
                        case 2:
                            appDto.setShowDevice("苹果");
                        case 3:
                            appDto.setShowDevice("电脑客户端");
                            break;
                        default:
                            break;
                    }
                }
                return appDto;
            }).collect(Collectors.toList());
            dto.setAppList(appDtoList);
        }
        return dto;
    }
}
